<script setup lang="ts">
import { ref, onMounted } from "vue";
import { getBalancePaymentOrders } from "@/api/balance";
import { ElMessage } from "element-plus";

const balancePaymentOrders = ref<any[]>([]);
const loading = ref(false);
const pagination = ref({
  page: 1,
  page_size: 10,
  total: 0,
  total_page: 0
});

const fetchBalancePaymentOrders = async () => {
  loading.value = true;
  try {
    const res = await getBalancePaymentOrders({
      page: pagination.value.page,
      page_size: pagination.value.page_size
    });
    if (res.success) {
      balancePaymentOrders.value = res.data;
      pagination.value = res.pagination;
    } else {
      ElMessage.error(res.message || "获取余额支付订单列表失败");
    }
  } catch (error) {
    ElMessage.error("获取余额支付订单列表失败");
  } finally {
    loading.value = false;
  }
};

const handlePageChange = (page: number) => {
  pagination.value.page = page;
  fetchBalancePaymentOrders();
};

const handlePageSizeChange = (pageSize: number) => {
  pagination.value.page_size = pageSize;
  pagination.value.page = 1;
  fetchBalancePaymentOrders();
};

const getStatusText = (status: number) => {
  switch (status) {
    case 0: return '待支付';
    case 1: return '已支付';
    case 2: return '支付失败';
    case 3: return '已取消';
    default: return '未知';
  }
};

const getStatusType = (status: number) => {
  switch (status) {
    case 0: return 'info';
    case 1: return 'success';
    case 2: return 'danger';
    case 3: return 'warning';
    default: return 'info';
  }
};

onMounted(() => {
  fetchBalancePaymentOrders();
});
</script>

<template>
  <div class="balance-payment-order-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>余额支付订单列表</span>
        </div>
      </template>
      
      <el-table :data="balancePaymentOrders" v-loading="loading" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="order_no" label="订单号" />
        <el-table-column prop="user_id" label="用户ID" />
        <el-table-column prop="amount" label="订单金额" />
        <el-table-column prop="balance_used" label="使用余额" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="180" />
        <el-table-column prop="paid_at" label="支付时间" width="180" />
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.page_size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handlePageChange"
          @size-change="handlePageSizeChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>